/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -10 -09
 * Time: 22:45
 */
public class test6 {
    static Object lock = new Object();
    static volatile int cnt = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    synchronized (lock) {
                        while(cnt % 3 != 0){
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        cnt++;
                        System.out.print("A");
                        lock.notifyAll();
                    }
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    synchronized (lock) {
                        while(cnt % 3 != 1){
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        cnt++;
                        System.out.print("B");
                        lock.notifyAll();
                    }
                }
            }
        };
        Thread t3 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    synchronized (lock) {
                        while(cnt % 3 != 2){
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        cnt++;
                        System.out.println("C");
                        lock.notifyAll();
                    }
                }
            }
        };

        t3.start();
        t2.start();
        t1.start();
    }
}
